package com.cnswhy.cloud.server.merchant.controller.cps;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.cnswhy.cloud.common.core.mybatis.conditions.Wraps;
import com.cnswhy.cloud.server.core.entity.CpsBankRate;
import com.cnswhy.cloud.server.core.entity.CpsTransOrder;
import com.cnswhy.cloud.server.core.dto.CpsTransOrderSaveDTO;
import com.cnswhy.cloud.server.core.dto.CpsTransOrderUpdateDTO;
import com.cnswhy.cloud.server.core.dto.CpsTransOrderPageDTO;
import com.cnswhy.cloud.server.core.service.CpsBankRateService;
import com.cnswhy.cloud.server.core.service.CpsTransOrderService;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import com.cnswhy.cloud.common.core.base.controller.SuperController;
import com.cnswhy.cloud.common.core.base.R;
import io.swagger.annotations.Api;
import lombok.extern.slf4j.Slf4j;
import lombok.var;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestMapping;
//import com.github.zuihou.security.annotation.PreAuth;
import org.springframework.web.bind.annotation.RestController;


/**
 * <p>
 * 前端控制器
 * 
 * </p>
 *
 * @author cnswhy
 * @date 2021-01-31
 */
@Slf4j
@Validated
@RestController
@RequestMapping("/cpsTransOrder")
@Api(value = "CpsTransOrder", tags = "")
//@PreAuth(replace = "cpsTransOrder:")
public class CpsTransOrderController extends SuperController<CpsTransOrderService, Long, CpsTransOrder, CpsTransOrderPageDTO, CpsTransOrderSaveDTO, CpsTransOrderUpdateDTO> {
    @Autowired
    CpsBankRateService cpsBankRateService;
    @Override
    public void handlerResult(IPage<CpsTransOrder> page) {
        for(var var : page.getRecords()){
            CpsBankRate cpsBankRate = cpsBankRateService.getOne(Wraps.<CpsBankRate>lbQ().eq(CpsBankRate::getBankAbbr, var.getBankAbbr()).eq(CpsBankRate::getChannelType, var.getChannelType()));
            var.setBankName(cpsBankRate.getBankName());
        }
    }

    /**
     * Excel导入后的操作
     *
     * @param list
     */
    @Override
    public R<Boolean> handlerImport(List<Map<String, String>> list){
        List<CpsTransOrder> cpsTransOrderList = list.stream().map((map) -> {
            CpsTransOrder cpsTransOrder = CpsTransOrder.builder().build();
            return cpsTransOrder;
        }).collect(Collectors.toList());

        return R.success(baseService.saveBatch(cpsTransOrderList));
    }
}
